package com.cxrj.zhsq.community.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cxrj.zhsq.community.service.CommunityService;

@Controller
public class CommunityController {

	@Resource
	private CommunityService communityService;

	/**
	 * 分页查询社区圈
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectCommunityCirclePager", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectCommunityCirclePager(HttpServletRequest request, HttpServletResponse response) {
		return communityService.selectCommunityCirclePager(request, response);
	}

	/**
	 * 根据id查询社区圈
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectCommunityCircleById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectCommunityCircleById(String id, HttpServletRequest request) {
		return communityService.selectCommunityCircleById(id);
	}

	/**
	 * 删除社区圈
	 * 
	 * @return
	 */
	@RequestMapping(value = "delCommunityCircle", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> delCommunityCircle(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = communityService.deleteCommunityCircle(id);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 删除回复
	 * 
	 * @return
	 */
	@RequestMapping(value = "delCommunityReply", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> delCommunityReply(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = communityService.deleteCommunityReply(id);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 添加回复
	 * 
	 * @return
	 */
	@RequestMapping(value = "insertCommunityReply", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> insertCommunityReply(String id, String replyContent, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			String name = request.getSession().getAttribute("userName").toString();
			resultMap = communityService.insertCommunityReply(id, name, "", replyContent);
			resultMap.put("name", name);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 添加社区圈
	 * 
	 * @param
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "insertCommunityCircle")
	@ResponseBody
	public Map<String, Object> insertCommunityCircle(@RequestParam("xstp") MultipartFile[] xstp, String content, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			/* 完成文件上传 获取图片路径组 */
			InputStream inputStream = null;
			String filePathAll = "";
			if (xstp != null) {
				for (int i = 0; i < xstp.length; i++) {
					if (xstp[i].getOriginalFilename() != null && xstp[i].getOriginalFilename().split("\\.").length > 1) {
						if (xstp[i].getSize() <= Integer.MAX_VALUE) {// 文件长度
							/* 文件保存路径 */
							String fileName = UUID.randomUUID().toString() + "." + xstp[i].getOriginalFilename().split("\\.")[1];
							String path = "upload" + File.separator + "zhsq_xg" + File.separator;
							String filePath = File.separator + request.getSession().getServletContext().getRealPath("/") + ".." + File.separator + path;
							inputStream = xstp[i].getInputStream();
							byte[] b = new byte[(int) xstp[i].getSize()];
							int read = 0;
							while ((read = inputStream.read(b)) != -1) {

							}
							inputStream.close();
							OutputStream os = new FileOutputStream(new File(filePath + File.separator + fileName));
							os.write(b);
							os.flush();
							os.close();
							filePathAll += "★" + fileName;
						}
					}
				}
			}
			resultMap = communityService.insertCommunityCircleByBackstage(request, content, filePathAll);
		} catch (Exception e) {
			resultMap.put("result", 0);
			resultMap.put("exception", e.getMessage());
		}
		return resultMap;
	}
	

	/**
	 * 根据id查询社区圈举报信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectCommunityComplaint", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectCommunityComplaint(String id, HttpServletRequest request) {
		return communityService.selectCommunityComplaint(id);
	}

}
